package com.qiangesoft.miniapp.config.ma.store;

import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.hutool.core.util.StrUtil;
import com.qiangesoft.miniapp.properties.WxMaProperties;

/**
 * 微信配置抽象
 *
 * @author qiangesoft
 * @since 2024-07-16
 */
public abstract class AbstractWxMaStorageConfiguration {

    /**
     * 配置
     *
     * @param config
     * @param properties
     * @return
     */
    protected WxMaDefaultConfigImpl config(WxMaDefaultConfigImpl config, WxMaProperties properties) {
        // 基本配置
        config.setAppid(StrUtil.trimToNull(properties.getAppid()));
        config.setSecret(StrUtil.trimToNull(properties.getSecret()));
        config.setToken(StrUtil.trimToNull(properties.getToken()));
        config.setAesKey(StrUtil.trimToNull(properties.getAesKey()));
        config.setMsgDataFormat(StrUtil.trimToNull(properties.getMsgDataFormat()));

        // 代理配置
        WxMaProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
        config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
        config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
        config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
        if (configStorageProperties.getHttpProxyPort() != null) {
            config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
        }

        int maxRetryTimes = configStorageProperties.getMaxRetryTimes();
        if (configStorageProperties.getMaxRetryTimes() < 0) {
            maxRetryTimes = 0;
        }
        int retrySleepMillis = configStorageProperties.getRetrySleepMillis();
        if (retrySleepMillis < 0) {
            retrySleepMillis = 1000;
        }
        config.setRetrySleepMillis(retrySleepMillis);
        config.setMaxRetryTimes(maxRetryTimes);
        return config;
    }

}

